#########################################################
#Figure C1: Median Home Value at LSOA
#########################################################

# Note this code requires 64 RAM.

rm(list=ls())
library(rgdal)
library(ggplot2)
library(maptools)
library(rgeos)
library(scales)
library(readstata13)

setwd("~/Dropbox/Replication/BJPS/")

myshp2 <- "./input/Lower_Layer_Super_Output_Areas_December_2011_Boundaries_EW_BFC.shp"
myfile2<-readOGR(myshp2)
df2 <- fortify(myfile2)
df2$id <- as.numeric(as.character(df2$id))
summary(df2$id)


data2<- myfile2@data
data2$FID <- as.numeric(as.character(data2$FID))
summary(data2$FID)
data2$id <- data2$FID-1
summary(data2$id)

merge <- merge(df2, data2, by="id")

homeprices <- read.dta13("./input/lsoa_median.dta")
homeprices$house_median_jun18 <- as.numeric(as.character(homeprices$house_median_jun18))
merge$lsoa_code <- merge$LSOA11CD

merge.final <- merge(merge, homeprices, by="lsoa_code")
merge.final$house_median_jun18 <- merge.final$house_median_jun18/ 1000 



ggplot(merge.final, aes(x=long, y=lat, group=group)) + geom_path() + coord_map("mercator") + 
  geom_polygon(aes(fill=house_median_jun18),  color = "papayawhip", size=.0) + 
  theme_minimal() + 
  xlab(" ") + ylab(" ") + 
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_blank())  + 
  scale_fill_gradientn(colors = #c("seagreen4", "palegreen1", "goldenrod1", "coral2", "red", "brown"),
                         # c("seagreen4", "green", "aquamarine" ,  "indianred2"    , "red", "brown"),
                         c("steelblue4","lightblue4", "lightblue" ,  "lightpink" , "lightpink4" , "brown"),
                       
                       values = scales::rescale(c(0, 200, 400, 600 , 800, 1000)),
                       breaks=c(200, 400, 600, 800, 1000),
                       limits=c(0,1000),
                       
                       
                       name="Home Price", na.value = "gray")
ggsave("./fig_c1.jpeg")





